import {
  Body,
  Controller,
  Get,
  Inject,
  Post,
  Provide,
  ALL,
} from '@midwayjs/core';
import { R } from '../../../common/base.error.util';

// import { AuthDTO } from '../dto/auth';
import { AuthService } from '../service/auth';
import { CaptchaService } from '../service/captche';
import { ApiResponse } from '@midwayjs/swagger';
import { TokenVO } from '../vo/token';
import { LoginDTO } from '../dto/login';
@Provide()
@Controller('/auth')
export class AuthController {
  @Inject()
  authService: AuthService;
  @Inject()
  captchaService: CaptchaService;

  // @Post('/', { description: '新建' })
  // async create(@Body(ALL) data: AuthDTO) {
  //   return await this.authService.create(data.toEntity());
  // }

  @Post('/login', { description: '登录' })
  @ApiResponse({ type: TokenVO })
  async login(@Body(ALL) logionDTO: LoginDTO) {
    const { captcha, captchaId } = logionDTO;

    const result = await this.captchaService.check(captchaId, captcha);

    if (!result) {
      throw R.error('验证码错误');
    }

    return await this.authService.login(logionDTO);
  }
  @Get('/captcha')
  async getImageCaptcha() {
    const { id, imageBase64 } = await this.captchaService.formula({
      height: 40,
      width: 120,
      noise: 1,
      color: true,
    });
    return {
      id,
      imageBase64,
    };
  }
}
